<?php
/**
 * Show module infos
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @category   Module
 * @uses       Zend_Controller_Front
 * @package    User
 * @subpackage Model
 * @todo
 */

class Access_Model_Module extends Zend_Db_Table_Abstract
{
    /**
     * Get module as list
     * 
     * @return array Module list
     */
    public function getModuleList()
    { 
        $front = Zend_Controller_Front::getInstance();
        $modules = $front->getControllerDirectory();
        $appData = array();
        
        foreach ($modules as $module => $path) {
            
            $realPath = realpath(
                $path . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR .
                "configs" . DIRECTORY_SEPARATOR . "app.ini"
            );
        
            if($realPath) {

                $handle = new Zend_Config_Ini($realPath);
                $config = $handle->toArray();
                $appData[$module]['controller'] = array();
                $appData[$module]['action'] = array();

                if(isset($config['app'])) {
                    $appData[$module]['app'] = $config['app'];
                } else {
                    $appData[$module]['app'] = $module;
                }
                
                if(isset($config['controllers'])) {

                    foreach ($config['controllers'] as $language => $controller) {
                        $appData[$module]['controller'] = $controller;
                        break;
                    }
                }
                unset($language);
                unset($controller);
                if(isset($config['actions'])) {
                    foreach ($config['actions'] as $language => $action) {
                        $appData[$module]['action'] = $action;
                        break;
                    }
                }
                unset($language);
                unset($controller);
            }
            
        }
        return $appData;
    }
    public function getAccessList($roleId)
    {
        $moduleList = $this->getModuleList();

        $query = $this->_db
        ->select()
        ->from('access_rules',
            array(
                'id',
                'resource',
                'privileg',
            )
        )
        ->where('roles_id' . ' = ?', $roleId);
        $accessList = $this->_db->fetchAll($query);
        if(empty($accessList)) {
          return $moduleList;
        }
        // add access of privileges
        foreach ($accessList as $accessElement) {
    
          if($accessElement['resource'] == 'all') {
            return 'administrator';
          }
    
          $resource = explode('_', $accessElement['resource']);
          $module = $resource[0];
          $controller = $resource[1];
          $privileges = explode(',', $accessElement['privileg']);
    
          foreach ($privileges as $privileg) {
            $moduleList[$module]['access'][$controller][$privileg] = 'allow';
          }
        }
        return $moduleList;
    
    }
}